package com.commerce.afive.Retrofit;

import androidx.appcompat.app.AppCompatActivity;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.commerce.afive.Entity.Bean;
import com.commerce.afive.R;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;
import retrofit2.http.QueryMap;

public class RetrofitActivity extends AppCompatActivity {
    EditText textView;
    TimerTask timerTask;
    private Timer timer;
    Call<Bean> call;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrofit);
        textView = findViewById(R.id.rt_tv_text);
        getText();
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                task(call.clone());
            }
        });
//        timer=new Timer();
//        timerTask=new TimerTask() {
//            @Override
//            public void run() {
//
//            }
//        };
//        timer.schedule(timerTask,0,3000);
    }


    //请求接口 2k  https://www.cnblogs.com/javaktolin/p/13609507.html
    public interface ApiService{
        @GET(" p/13609507.html")
        Call<ResponseBody> getHtml();
    }
    //@Path 动态配置URL地址
    public interface ApiServicePath{
        @GET("{path}/13609507.html")
        Call<ResponseBody> getHtml(@Path("path") String str);
    }
    //@Body 传输数据类型Json字符串
    public interface ApiServiceBody{
        @POST("p/13609507.html")
        Call<ResponseBody> getHtml(@Body String s);
    }
    //@Field  传输数据类型键值对 https://www.sogou.com/sogou?query=okhttp
    public interface ApiServiceField{
        @FormUrlEncoded
        @POST("sogou")
        Call<ResponseBody> getHtml(@Field("query") String query);
    }
    //@Query 动态指定查询条件 http://ip.taobao.com/service/getIpInfo.php?ip=59.108.54.37
    public interface ApiServiceQuery{
        @GET("getIpInfo.php")
        Call<ResponseBody> getHtml(@Query("ip") String ip);
    }
    //@QueryMap 动态指定查询条件组
    public interface ApiServiceQueryMap{
        @GET("getIpInfo.php")
        Call<Bean> getHtml(@QueryMap Map<String,String> query);
    }
    private String str1 = "https://www.cnblogs.com/javaktolin/";
    private String str2 = "https://www.sogou.com/";
    private String str3 = "http://ip.taobao.com/service/";
    public  void getText(){
        //创建Retrofit (okHttp3)34效率5并socket 5
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(str3)
                .addConverterFactory(GsonConverterFactory.create())//Gson解析
                .build();
        //创建Retrofit实例
        ApiServiceQueryMap apiService = retrofit.create(ApiServiceQueryMap.class);
        //生成接口的动态代理对象
        Map<String,String> map = new HashMap<>();
        map.put("ip","59.108.54.37");
        call = apiService.getHtml(map);
        //同步请求网络
//        call.execute();
        //中断网络
//        call.cancel();
        //异步请求网络
        task(call);
    }
    private void task(Call<Bean> call){
        call.enqueue(new Callback<Bean>() {
            @Override
            public void onResponse(Call<Bean> call, Response<Bean> response) {
                Bean body = response.body();
                textView.setText(body.getCode()+"22");
                call.cancel();
            }

            @Override
            public void onFailure(Call<Bean> call, Throwable t) {
                textView.setText("onFailure");
            }
        });
    }
}